"""
给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点

Given the head of a linked list, remove the nth node from the end of the list and return its head.

"""
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution(object):
    """
        创建一个头节点
        先遍历获得链表长度
        遍历找到删除节点的前驱，删除节点返回头节点的后继
    """
    def removeNthFromEnd(self, head, n):
        length = 0
        while head:
            length += 1
            head = head.next
        headNode = ListNode(0, head)
        temp = headNode
        for i in range(1, length - n + 1):
            temp = temp.next
        temp.next = temp.next.next
        return headNode.next